﻿using ZyGame.Execute;
using ZyGame.Resource;
using System;
using System.Collections.Generic;
using System.IO;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace ZyGame.UI
{
    public class UIWindow : IDisposable
    {
        private Action _evt_enable;
        private Action _evt_disable;
        private Action _evt_close;
        public string name { get; internal set; }

        public GameObject gameObject { get; internal set; }

        private Dictionary<string, GameObject> cache = new Dictionary<string, GameObject>();

        private Dictionary<string, UIListView> viewLists = new Dictionary<string, UIListView>();

        public UIWindow()
        {
        }

        public void OnListenerEnableCompletion(Action callback)
        {
            _evt_enable += callback;
        }

        public void OnListenerDisableCompletion(Action callback)
        {
            _evt_disable += callback;
        }

        public void OnListenerCloseCompletion(Action callback)
        {
            _evt_close += callback;
        }

        public virtual void Awake(params object[] paramsList)
        {
            RectTransform[] rectTransforms = gameObject.GetComponentsInChildren<RectTransform>(true);
            for (int i = 0; i < rectTransforms.Length; i++)
            {
                if (cache.ContainsKey(rectTransforms[i].name))
                {
                    continue;
                }

                cache.Add(rectTransforms[i].name, rectTransforms[i].gameObject);
            }
        }

        public virtual void Dispose()
        {
            if (gameObject == null)
            {
                return;
            }
            _evt_close?.Invoke();
            GameObject.DestroyImmediate(gameObject);
        }

        public GameObject GetChild(string name)
        {
            if (cache.TryGetValue(name, out GameObject gameObject))
            {
                return gameObject;
            }

            cache[name] = this.gameObject.FindChild(name);

            return cache[name];
        }


        public virtual void Enable()
        {
            _evt_enable?.Invoke();
            this.gameObject.SetActive(true);
        }

        public virtual void Disable()
        {
            _evt_disable?.Invoke();
            this.gameObject.SetActive(false);
        }

        /// <summary>
        /// 设置列表
        /// </summary>
        /// <param name="view"></param>
        /// <param name="name"></param>
        /// <param name="tempName"></param>
        /// <returns></returns>
        internal UIListView GetOrAddListView(string name, string tempName)
        {
            UIListView listView = GetListView(name);
            if (listView != null)
            {
                return listView;
            }

            GameObject root = GetChild(name);
            GameObject temp = GetChild(tempName);
            if (root == null || temp == null)
            {
                throw new System.ArgumentNullException();
            }

            viewLists.Add(name, listView = new UIListView(root, temp));
            return listView;
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="view"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        internal UIListView GetListView(string name)
        {
            if (viewLists.TryGetValue(name, out UIListView listView))
            {
                return listView;
            }

            return default;
        }

        /// <summary>
        /// 移除列表
        /// </summary>
        /// <param name="view"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        internal UIWindow RemoveListView(string name)
        {
            UIListView listView = GetListView(name);
            if (listView != null)
            {
                listView.Dispose();
                viewLists.Remove(name);
            }

            return this;
        }
    }
}